package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateUpdateInfoGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUpdateUpdateInfoDTO;
import com.itlong.cloud.POJO.PO.DeviceCloudUpdatePO;
import com.itlong.cloud.POJO.PO.OperateUserInfoPO;
import com.itlong.cloud.POJO.PO.OperationSpecialLogPO;
import com.itlong.cloud.POJO.VO.operate.DeviceTypeDefineVO;
import com.itlong.cloud.POJO.VO.operate.OperateUpdateDetailVO;
import com.itlong.cloud.POJO.VO.operate.OperateUpdateInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperatePackageDao;
import com.itlong.cloud.operate.dao.OperationSpecialLogDao;
import com.itlong.cloud.operate.service.IOperatePackageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * <desc>
 *      运营-固件升级-安装包管理服务接口实现类。
 * <desc/>
 *
 * @createDate 2018/08/23.
 */
@Service
public class IOperatePackageServiceImpl implements IOperatePackageService {

    private static final Logger LOG = LoggerFactory.getLogger(IOperatePackageServiceImpl.class);

    @Autowired
    IOperatePackageDao iOperatePackageDao;

    @Autowired
    private OperationSpecialLogDao operationSpecialLogDao;
    /**
     * <desc>
     *      分页获取安装包信息。
     * <desc/>
     *
     * @param  operateUpdateInfoGetPageDTO 参数请求对象
     * @return
     * @author Qiang.S
     * @createDate 2018/08/23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateUpdateInfoVO> getUpdateInfoPage(OperateUpdateInfoGetPageDTO operateUpdateInfoGetPageDTO) throws Exception{
        if(StringUtils.isBlank(operateUpdateInfoGetPageDTO.getOrderColumn())){
            operateUpdateInfoGetPageDTO.setOrderColumn("createTime");
            operateUpdateInfoGetPageDTO.setOrderRule("DESC");
        }
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateUpdateInfoGetPageDTO);
        //分页类默认pageSize为20
        Page<OperateUpdateInfoVO> page = new Page<>();
        page.getPage().setPageSize(operateUpdateInfoGetPageDTO.getPageSize());
        page.getPage().setCurrentPage(operateUpdateInfoGetPageDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperatePackageDao.getUpdateInfoPage(params),iOperatePackageDao.getUpdateInfoCount(params));
        return page;
    }

    /**
     * <desc>
     *      批量删除安装包信息
     * <desc/>
     *
     * @param updateIds 更新文件编号,逗号分隔
     * @return
     * @author Qiang.S
     * @createDate 2018-08-23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer deleteUpdateInfo(String updateIds, String userId,
                                    String operateFunction, String operateWay,String updateNames , String ip ) throws Exception {
        Date nowTime = new Date();
        String[] updateId = null;
        String[] updateName = null;
        if (updateIds != null && !"".equals(updateIds)) {
            updateId = updateIds.split(",");
        }
        if (updateNames != null && !"".equals(updateNames)) {
            updateName = updateIds.split(",");
        }
        iOperatePackageDao.deleteCloudChange(updateId);
        iOperatePackageDao.deleteElevatorChange(updateId);
        Integer i = iOperatePackageDao.deleteUpdateInfo(updateId);
        if (i>0){
           for (String id: updateId){
               for (String name : updateId){
                   //新增操作日志
                   OperateUserInfoPO operateUserInfoPO = operationSpecialLogDao.getOperateUserPO(userId);
                   OperationSpecialLogPO operationSpecialLogPO = new OperationSpecialLogPO();
                   operationSpecialLogPO.setLogId(LogicIdUtil.bussinessId());
                   operationSpecialLogPO.setRealName(operateUserInfoPO.getRealName());
                   operationSpecialLogPO.setAccount(operateUserInfoPO.getUserName());
                   operationSpecialLogPO.setOuterId(id);
                   operationSpecialLogPO.setIp(ip);
                   operationSpecialLogPO.setOperateFunction(operateFunction);
                   operationSpecialLogPO.setOperateWay(operateWay);
                   operationSpecialLogPO.setFunctionType(7);
                   operationSpecialLogPO.setOperateDec(operateUserInfoPO.getRealName() + "删除了"+ name +"安装包");
                   operationSpecialLogPO.setStatus(DataStatusEnum.NORMAL.getType());
                   operationSpecialLogPO.setCreateTime(nowTime);
                   operationSpecialLogPO.setUpdateTime(nowTime);
                   operationSpecialLogDao.save(SqlUtil.durableData(operationSpecialLogPO,PlatformConstants.TABLE_SAVE));
               }
           }

        }
        return i;
    }

    /**
     * <desc>
     *      根据名称查询结果
     * <desc/>
     *
     * @param updateName 文件名称
     * @return
     * @author Qiang.S
     * @createDate 2018-08-23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String getUpdateInfoByName(String updateName) throws Exception {
        return iOperatePackageDao.getUpdateInfoByName(updateName);
    }

    /**
     * <desc>
     *      获取安装包详细信息。
     * <desc/>
     *
     * @param updateId 更新文件编号
     * @return
     * @author Qiang.S
     * @createDate 2018/08/23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateUpdateDetailVO getUpdateDetail(@RequestParam("updateId") String updateId) throws Exception {
        OperateUpdateDetailVO updateDetail = iOperatePackageDao.getUpdateDetail(updateId);
        return updateDetail;
    }

    /**
     * <desc>
     *      安装包信息更新(未更新安装文件时使用)
     * <desc/>
     *
     * @param operateUpdateUpdateInfoDTO 更新安装包信息
     * @return
     * @author Qiang.S
     * @createDate 2018-08-28
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer updateUpdateInfo(OperateUpdateUpdateInfoDTO operateUpdateUpdateInfoDTO) throws Exception {

        OperateUpdateDetailVO updateDetail = iOperatePackageDao.getUpdateDetail(operateUpdateUpdateInfoDTO.getUpdateId());
        Date nowTime = new Date();
        DeviceCloudUpdatePO deviceCloudUpdatePO = new DeviceCloudUpdatePO();
        deviceCloudUpdatePO.setUpdateId(operateUpdateUpdateInfoDTO.getUpdateId());
        deviceCloudUpdatePO.setDeviceType(operateUpdateUpdateInfoDTO.getDeviceType());
        deviceCloudUpdatePO.setVersion(operateUpdateUpdateInfoDTO.getVersion());
        deviceCloudUpdatePO.setUpdateName(updateDetail.getUpdateName());
        deviceCloudUpdatePO.setTypePid(operateUpdateUpdateInfoDTO.getTypePid());
        deviceCloudUpdatePO.setRemark(operateUpdateUpdateInfoDTO.getRemark());
        deviceCloudUpdatePO.setOperateWay(operateUpdateUpdateInfoDTO.getOperateWay());
        deviceCloudUpdatePO.setOperateFunction(operateUpdateUpdateInfoDTO.getOperateFunction());
        if ("%nodata%".equals(operateUpdateUpdateInfoDTO.getRemark())) {
            deviceCloudUpdatePO.setRemark("");
        }
        if ("%nodata%".equals(operateUpdateUpdateInfoDTO.getTypePid())) {
            deviceCloudUpdatePO.setTypePid("");
        }
        deviceCloudUpdatePO.setUpdateTime(new Date());
        Map<String, Object> params = SqlUtil.durableData(deviceCloudUpdatePO, PlatformConstants.TABLE_UPDATE);
        for(String key : params.keySet()){
            if (params.get(key) != null && "".equals(params.get(key))) {
                params.put(key,null);
            }
        }
        Integer i = iOperatePackageDao.updateUpdateInfo(params);
        if (i>0){
            //新增操作日志
            OperateUserInfoPO operateUserInfoPO = operationSpecialLogDao.getOperateUserPO(operateUpdateUpdateInfoDTO.getLoginUserId());
            OperationSpecialLogPO operationSpecialLogPO = new OperationSpecialLogPO();
            operationSpecialLogPO.setLogId(LogicIdUtil.bussinessId());
            operationSpecialLogPO.setRealName(operateUserInfoPO.getRealName());
            operationSpecialLogPO.setAccount(operateUserInfoPO.getUserName());
            operationSpecialLogPO.setOuterId(deviceCloudUpdatePO.getUpdateId());
            operationSpecialLogPO.setIp(operateUpdateUpdateInfoDTO.getIp());
            operationSpecialLogPO.setOperateFunction(deviceCloudUpdatePO.getOperateFunction());
            operationSpecialLogPO.setOperateWay(deviceCloudUpdatePO.getOperateWay());
            operationSpecialLogPO.setFunctionType(7);
            operationSpecialLogPO.setOperateDec(operateUserInfoPO.getRealName() + "修改了"+ deviceCloudUpdatePO.getUpdateName() +"安装包");
            operationSpecialLogPO.setStatus(DataStatusEnum.NORMAL.getType());
            operationSpecialLogPO.setCreateTime(nowTime);
            operationSpecialLogPO.setUpdateTime(nowTime);
            Map<String, Object> map = SqlUtil.durableData(operationSpecialLogPO, PlatformConstants.TABLE_SAVE);
            operationSpecialLogDao.save(map);
        }

        return i;
    }

    /**
     * <desc>
     * 根据产品类型查询产品型号
     * </desc>
     *
     * @param deviceTypes
     * @Author chengshican
     * @Date 2020-11-11 15:49
     * @Param deviceTypes
     * @Return
     * @Exception
     */
    @Override
    public List<DeviceTypeDefineVO> getProductCodeByDeviceType(String deviceTypes) throws Exception {
        String[] arr=deviceTypes.split(",");
        List<String> list = new ArrayList<>();
        list.addAll(Arrays.asList(arr));
        boolean flag = list.contains("2");
      if (flag){
          list.add("7");
          String[] array = list.toArray(new String[list.size()]);
          return iOperatePackageDao.getProductCodeByDeviceType(array);
      }else {
          return iOperatePackageDao.getProductCodeByDeviceType(arr);
      }


    }


}
